/**
 * losenote.com Inc.
 * Copyright (c) 2013-2013 All Rights Reserved.
 */
package com.losenote.core.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import com.losenote.core.CategoryComponent;
import com.losenote.core.ContactDetailComponent;
import com.losenote.core.FindNoteComponent;
import com.losenote.model.CategoryItem;
import com.losenote.model.ContactDetail;
import com.losenote.model.FindNote;
import com.losenote.model.OperateContext;
import com.losenote.model.ct.FindNoteConvertor;
import com.losenote.orm.ContactDetailDODAO;
import com.losenote.orm.FindNoteDO;
import com.losenote.orm.FindNoteDODAO;
import com.losenote.orm.QuestionDO;
import com.losenote.orm.QuestionDODAO;
import com.losenote.service.req.FindNodePubReq;

/**
 * 招领告示组件操作對象
 * 
 * @author danjingyu
 * @version $Id: FindNoteComponentImpl.java, v 0.1 2013-10-26 下午4:32:33 danjingyu Exp $
 */
public class FindNoteComponentImpl implements FindNoteComponent {

    @Autowired
    private FindNoteDODAO          findNoteDODAO;
    @Autowired
    private QuestionDODAO          questionDODAO;
    @Autowired
    private ContactDetailDODAO     contactDetailDODAO;
    @Autowired
    private ContactDetailComponent contactDetailComponent;
    @Autowired
    private CategoryComponent      categoryComponent;

    /** 
     * @see com.losenote.core.FindNoteComponent#publishFindNote(com.losenote.model.FindNote, com.losenote.model.OperateContext)
     */
    public int publishNote(FindNodePubReq req, OperateContext operateContext) {

        //【1】将用户的联系信息保存
        int contactId = contactDetailDODAO.insert(req.getContactDo());
        req.getFindNoteDO().setContactId(contactId);

        //【1】保存招贴告示信息
        long id = findNoteDODAO.insert(req.getFindNoteDO());

        //【2】在用户设定匿名模式下，保存用户确认的问题
        if (!CollectionUtils.isEmpty(req.getQuestionDos())) {
            for (QuestionDO questionDO : req.getQuestionDos()) {
                questionDO.setRelId((int) id);
                questionDODAO.insert(questionDO);
            }
        }
        return (int) id;
    }

    /** 
     * @see com.losenote.core.FindNoteComponent#queryById(int)
     */
    @Override
    public FindNote queryById(int noteId) {
        FindNoteDO findNoteDO = findNoteDODAO.selectByPrimaryKey(noteId);
        if (findNoteDO == null) {
            return null;
        }
        ContactDetail contactDetail = contactDetailComponent.getById(findNoteDO.getContactId());
        CategoryItem categoryItem = categoryComponent.queryCategoryId(findNoteDO
            .getCategoryTypeId());
        return FindNoteConvertor.fromDO(findNoteDO, categoryItem, contactDetail);
    }

    public void setFindNoteDODAO(FindNoteDODAO findNoteDODAO) {
        this.findNoteDODAO = findNoteDODAO;
    }

    public void setQuestionDODAO(QuestionDODAO questionDODAO) {
        this.questionDODAO = questionDODAO;
    }

    public void setContactDetailDODAO(ContactDetailDODAO contactDetailDODAO) {
        this.contactDetailDODAO = contactDetailDODAO;
    }

    public void setContactDetailComponent(ContactDetailComponent contactDetailComponent) {
        this.contactDetailComponent = contactDetailComponent;
    }

    public void setCategoryComponent(CategoryComponent categoryComponent) {
        this.categoryComponent = categoryComponent;
    }

}
